﻿var Util = (function () {
	var that;
	var obj = function () {
		that = this;
		this.childPageIdList = [];
	};

	obj.prototype = {
	    getDeviceType: function () {
	        // pc:0 ios:1 android:2 其他:-1
	        if (!("ontouchstart" in window)) {
	            return 0;
	        }

	        if (!!navigator.userAgent.match(/(i[^;]+\;(U;)? CPU.+Mac OS X)/)) {
	            return 1;
	        }

	        if (navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1) {
	            return 2;
	        }

	        return -1;
	    },

		navChange: function (index) {
			var srcs = ["folder.html", "art.html", "", "msg.html", "set.html"];
			var src = srcs[index];
			var deviceType = that.getDeviceType();
			switch (deviceType) {
				case 0:
					location.href = src;
					break;
				case 1:
				case 2:
					CommonUtil.showMainWebView(src);
					break;
				default:
					break;
			}
			
		},

		getScreeningData: function () {
			var screeningData = [];
			var text = ["素描", "色彩", "速写", "国画", "油画", "插画", "动漫", "平面", "工业", "环艺", "服装", "摄影", "其他"];
			for (var i = 21, j = 0; i <= 33; i++, j++) {
				screeningData.push({
					src: "image/kong/" + i + ".jpg",
					title: text[j]
				});
			}
			return screeningData;
		},

		showChildPage: function (src) {
			var deviceType = that.getDeviceType();
		    switch (deviceType) {
		        case 0:
		            that._showChildPagePC(src);
		            break;
		    	case 1:
		    	case 2:
		    		CommonUtil.showChildWebView(src, function () { });
		    		break;
		        default:
		            break;
		    }
		},

		hideChildPage: function () {
			var deviceType = that.getDeviceType();
			switch (deviceType) {
				case 0:
					that._hideChildPagePC();
					break;
				case 1:
				case 2:
					CommonUtil.hideChildWebView();
					break;
				default:
					break;
			}
			
		},

		_showChildPagePC: function (src) {
		    var iframe = document.createElement("iframe");
		    iframe.className = "child_page";
		    iframe.id = that._getChildPageId();
		    iframe.frameBorder = 0;
		    iframe.src = that._addRandom(src);
		    iframe.type = "show";
		    iframe.addEventListener("webkitTransitionEnd", function () {
		        if (this.type == "hide") {
		            $(this).remove();
		        }
		    }, false);
		    top.document.body.appendChild(iframe);

		    iframe.onload = function () {
		        iframe.className += " show";
		    };
		},

		_hideChildPagePC: function () {
			var topPageId = top.Util.childPageIdList.pop();
			var topIframe = top.document.getElementById(topPageId);
			topIframe.type = "hide";
			topIframe.className += " hide";
		},

		_addRandom: function (src) {
			if (src.indexOf("?") < 0) {
				src += "?";
			} else {
				src += "&";
			}
			return src + "t=" + Math.random();
		},

		_getChildPageId: function () {
			var id = "cp" + Math.random().toString().substring(2);
			top.Util.childPageIdList.push(id);
			return id;
		}
	};

	return new obj();
})();